package sk.stuba.fiit.clientApp.gui.handler;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JTextField;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

import sk.stuba.fiit.clientApp.controller.ClientUtil;
import sk.stuba.fiit.clientApp.gui.panel.BasicPanel;

/**
 * Trieda zodpovedná za otvorenie dialogovéhjo okná ktoré ponúkne otvoriť súbor,
 * má referenciu na otvorený súbor + cestu do zapisuje do JTextFieldu
 * @author Marek Hruška
 *
 */
public class OpenInputFileListener implements ActionListener {
	private JTextField field;
	private File file;
	private boolean mode;
	private int modeValue;
	
	
	public OpenInputFileListener(JTextField textField, File file) {
		this(textField,file,false,0);
		
	}
	
	public OpenInputFileListener(JTextField textField, File file,boolean mode, int modeValue) {
		field = textField;
		this.file = file;
		this.mode = mode;
		this.modeValue = modeValue;
		
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		  if(file == null){
			  file = new File("./");
		  }

		  JFileChooser chooser = new JFileChooser(file);
		  if(!mode){
		     chooser.setMultiSelectionEnabled(false);
		     int option = chooser.showOpenDialog(null);	     
		     if (option == JFileChooser.APPROVE_OPTION) {
		       File sf = chooser.getSelectedFile();
		          field.setText(sf.getAbsolutePath());
		     }
		  }else if(modeValue == ClientUtil.OUTPUT_PACKAGE_VALUE){
				  chooser.setMultiSelectionEnabled(false);
				  chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				  int option = chooser.showOpenDialog(null);
				  if (option == JFileChooser.APPROVE_OPTION) {
				       File sf = chooser.getSelectedFile();
				          field.setText(sf.getAbsolutePath()+File.separator+ClientUtil.OUTPUT_PACKAGE);
				     }
		  }else if(modeValue == ClientUtil.XML_FILE_VALU){
			  chooser.setMultiSelectionEnabled(false);
			  chooser.addChoosableFileFilter(new FileFilter() {				
				@Override
				public String getDescription() {
					return "*.xml";
				}
				@Override
				public boolean accept(File f) {
			        return f.isDirectory() || f.getName().toLowerCase().endsWith(".xml");
				}
			});
			  int option = chooser.showOpenDialog(null);	     
			     if (option == JFileChooser.APPROVE_OPTION) {
			       File sf = chooser.getSelectedFile();
			       field.setText(sf.getAbsolutePath());
			     }
			  
		  }else if(modeValue == ClientUtil.TS_FILE_VALU){
			  chooser.setMultiSelectionEnabled(false);
			  chooser.addChoosableFileFilter(new FileFilter() {				
				@Override
				public String getDescription() {
					return "*.ts";
				}
				@Override
				public boolean accept(File f) {
			        return f.isDirectory() || f.getName().toLowerCase().endsWith(".ts");
				}
			});
			  int option = chooser.showOpenDialog(null);	     
			     if (option == JFileChooser.APPROVE_OPTION) {
			       File sf = chooser.getSelectedFile();
			       field.setText(sf.getAbsolutePath());
			     }
			  
		  }else if(modeValue == ClientUtil.ARCHIVE_PACKAGE_VALUE){
			  chooser.setMultiSelectionEnabled(false);
			  chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			  chooser.addChoosableFileFilter(new FileFilter() {				
				@Override
				public String getDescription() {
					return "Archive directory";
				}
				@Override
				public boolean accept(File f) {
			        return f.isDirectory();
				}
			});
			  int option = chooser.showOpenDialog(null);	     
			     if (option == JFileChooser.APPROVE_OPTION) {
			       File sf = chooser.getSelectedFile();
			       field.setText(sf.getAbsolutePath());
			     }
		  }

	}
	
	public JTextField getField() {
		return field;
	}

	public void setField(JTextField field) {
		this.field = field;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public boolean isMode() {
		return mode;
	}

	public void setMode(boolean mode) {
		this.mode = mode;
	}

	public int getModeValue() {
		return modeValue;
	}

	public void setModeValue(int modeValue) {
		this.modeValue = modeValue;
	}
}

